<?php defined('BASEPATH') OR exit('No direct script access allowed');


/**
 * 后台用户管理
 */
class Admin extends Admin_Controller {


	public function __construct()
	{
		parent::__construct();

	} 


	//列表页
	public function index()
	{
		$this->load->model('admin/admin_model', 'search_admin');

		//状态数组
        $data["status_arr"] = Admin_model::$status_arr;

		//分页
        $data["pageCurrent"] = ForceIntFrom('pageCurrent');
        $data["pageSize"] = ForceIntFrom('pageSize');

        $data["pageCurrent"] = Iif($data["pageCurrent"], $data["pageCurrent"], 1);
        $data["pageSize"] = Iif($data["pageSize"], $data["pageSize"], 20);

		//排序
        $data["orderField"] = trim($this->input->post('orderField', true));
        $data["orderDirection"] = trim($this->input->post('orderDirection', true));

        $data["orderField"] = Iif($data["orderField"], $data["orderField"], 'id');
        $data["orderDirection"] = Iif($data["orderDirection"], $data["orderDirection"], 'desc');


		//搜索关键字等
        $data['keytype'] = trim($this->input->post('keytype', true));
        $data['keyword'] = trim($this->input->post('keyword', true));

		//状态
		$data['status'] = trim($this->input->post('status', true));

		//搜索关键字等
        if(!empty($data['keyword']) && !empty($data['keytype']))
		{
            $this->search_admin->$data['keytype'] = $data['keyword'];
		}

        if(!empty($data['status']) && $data['status'] != "all")
		{
			$this->search_admin->status = $data['status'];
		}

		//读取所有的角色
		$data['admin_roles'] = array();
		$this->load->model('admin/role_model', 'search_role');
		$admin_roles = $this->search_role->get_all_roles();

		foreach($admin_roles AS $role)
		{
			$data['admin_roles'][$role->id] = $role->role_name;
		}


		//获取记录列表
		$this->search_admin->_order_by = "adur.{$data[orderField]} {$data[orderDirection]}";
        $this->search_admin->_page_index = $data["pageCurrent"];
        $this->search_admin->_page_count = $data["pageSize"];
		$this->search_admin->search();

        $data["list"] = $this->search_admin->_search_results;

		//获取总记录数
        $this->search_admin->_is_search_counter = true;
        $this->search_admin->search();

        $data["totalCount"] = $this->search_admin->_search_num_rows;


		$this->display('admin/index.html', $data);
	}


	//编辑管理员
	public function edit($id = 0)
	{
		$this->add($id);
	}


	//添加或编辑管理员
	public function add($id = 0)
	{
		$id = intval($id); //管理员id

		$data = array();

		//有id时加载管理员 -- 编辑
		if($id)
		{
			$this->load->model('admin/admin_model', 'search_admin');
			
			if(!$this->search_admin->retrieve($id))
			{
				$this->error('管理员不存在！');
			}

			//赋值
			$data['admin'] = $this->search_admin;
		}

		//读取所有的角色
		$this->load->model('admin/role_model', 'search_role');
		$data['admin_roles'] = $this->search_role->get_all_roles();
		$data['admin_roles'] = array_reverse($data['admin_roles']);

		$this->display('admin/edit.html', $data);
	}


	//保存(添加或更新)
	public function save()
	{
		$id = ForceIntFrom('id'); //用户id
		$status = ForceIntFrom('status'); //状态
		$role_id = ForceIntFrom('role_id'); //角色id

		$username = trim($this->input->post('username', true)); //用户名
		$password = trim($this->input->post('password', true));
		$re_password = trim($this->input->post('re_password', true));
		$nickname = trim($this->input->post('nickname', true));
		$mobile = trim($this->input->post('mobile', true));
		$email = trim($this->input->post('email', true));


		if(empty($username) OR empty($nickname) OR empty($mobile) OR empty($email) )
		{
			$this->error('表单数据不完整！');
		}

		if($id){
			if(!empty($password) OR !empty($re_password)){
				if(strcmp($password, $re_password)){
					$this->error('两次输入的密码不相同！');
				}
			}
		}else{
			if(empty($password)){
				$this->error('请输入密码！');
			}elseif($password != $re_password){
				$this->error('两次输入的密码不相同！');
			}
		}


		//加载后台用户model
		$this->load->model('admin/admin_model', 'edit_admin');

		//根据用户名及用户id判断用户名是否已被占用
		if($this->edit_admin->check_exist_by_username_or_id($username, $id))
		{
			$this->error('用户名已被占用！');
		}

		//根据email及用户id判断email是否已被占用
		if($this->edit_admin->check_exist_by_email_or_id($email, $id))
		{
			$this->error('邮箱已被占用！');
		}


		$this->edit_admin->username = $username;
		$this->edit_admin->nickname = $nickname;
		$this->edit_admin->mobile = $mobile;
		$this->edit_admin->email = $email;


		if($id) { //更新数据

			//不允许改变: 超级管理员 或 自己的状态和角色
			if($id != 1 AND $id != $this->admin->id)
			{
				$this->edit_admin->status = $status;
				$this->edit_admin->role_id = $role_id;
			}

			//编辑用户时如果填写了密码则更新
			if(!empty($password))
			{
				$salt = PassGen(6); //生成6位密码混淆字符串
				$this->edit_admin->salt = $salt;
				$this->edit_admin->password = md5($salt . md5($password));
			}

			$this->edit_admin->id = $id;

			if($this->edit_admin->update())
			{
				$this->success('管理员 -> 更新成功！'); //编辑时不更新tab
			}

		}else{ //插入记录

			$this->edit_admin->status = $status;
			$this->edit_admin->role_id = $role_id;
			$this->edit_admin->logins = 0;
			$this->edit_admin->last_ip = 0;
			$this->edit_admin->last_time = 0;
			$this->edit_admin->create_time = time();

			$salt = PassGen(6); //生成6位密码混淆字符串

			$this->edit_admin->salt = $salt;
			$this->edit_admin->password = md5($salt . md5($password));

			if($this->edit_admin->insert())
			{
				$this->success('管理员 -> 添加成功！', 'admin-admin-list'); //admin-admin-list指需要更新的tabId
			}
		}

		$this->error();
	}


	//删除
	public function delete($id = 0)
	{
		$id = intval($id); //id

		//不允许删除: 超级管理 或 自己
		if(empty($id) OR $id == '1' OR $id == $this->admin->id)
		{
			$this->error('未选择需要删除的管理员！');
		}

		$this->load->model('admin/admin_model');

		$this->admin_model->_delete_where = array('id' => $id);

		if($this->admin_model->delete())
		{
			$this->success('管理员 -> 删除成功！', 'admin-admin-list', false); //false表示: 不关闭当前tab或dialog
		}

		$this->error();
	}

}
